package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.ErrorCode;
import com.visizen.wisdomclass.server.action.response.ErrorCodeManager;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.model.dto.LoginLogExt;
import com.visizen.wisdomclass.server.service.LoginLogService;
import com.visizen.wisdomclass.server.token.WCToken;
import com.visizen.wisdomclass.server.token.WCTokenManager;
import com.visizen.wisdomclass.server.util.WCDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户 用户登录日志
 *
 * @author: chenjian
 */
@Controller
@RequestMapping("/User")
public class UserLoginLogAction {
    /**
     * 获取用户的登录日志
     *
     * @param token
     * @param count      获取日志的数量
     * @param startIndex 分页获取的页面索引
     * @param request
     * @return
     */
    @ValidateToken
    @RequestMapping(value = "/getLoginLog", method = RequestMethod.GET)
    @ResponseBody
    public Result getLoginLog(@RequestParam(required = true) String token,
                              @RequestParam(required = true) int count,
                              @RequestParam(required = true) int startIndex,
                              HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        JSONObject jo = new JSONObject();
        /**
         * 从token中获取数据
         */
        WCToken wcToken = tokenManager.getToken(token);
        WcUser user = (WcUser) wcToken.getSource();
        List<LoginLogExt> loginLogs = loginLogService.getLoginLog(user.getUser_id(), count, startIndex);
        if (null == loginLogs || loginLogs.size() == 0) {
            /**
             * 没有获取到用户登录日志信息
             */
            ErrorCodeManager codeManager = ErrorCodeManager.getLocalMsg(ErrorCode.C1018_getUserLoginLogFailed);
            jo.put("exception", "");
            result.setFailedResult(request, codeManager.getCode(), codeManager.getMsg(), jo);

            /**
             * 将数据设置到JSON对象中，返回前台
             */
            List<JSONObject> jsonLogs = new ArrayList<JSONObject>();
            JSONObject jsonLog;
            for (LoginLogExt log : loginLogs) {
                jsonLog = new JSONObject();
                jsonLog.put("logId", log.getLog_login_id());
                jsonLog.put("userId", log.getUser_id());
                jsonLog.put("equipId", log.getEquip_id());
                jsonLog.put("equipUUID", log.getEquipment().getEquip_uuid());
                jsonLog.put("logType", log.getLog_login_type());
                jsonLog.put("logTime", WCDate.toDateTimeString(log.getLog_login_time()));
                jsonLogs.add(jsonLog);
            }
            jo.put("resultCount", loginLogs.size());
            jo.put("loginLogList", jsonLogs);
            result.setSuccessResult(request, "", jo);
        }
        return result;
    }

    private WCTokenManager tokenManager;
    private LoginLogService loginLogService;

    @Autowired
    public void setLoginLogService(LoginLogService loginLogService) {
        this.loginLogService = loginLogService;
    }

    @Autowired
    public void setTokenManager(WCTokenManager tokenManager) {
        this.tokenManager = tokenManager;
    }
}
